<template>
  <div class="main">
    <div class="title">
      <a href="/login">登录</a>
      <span>·</span>
      <a class="active" href="/register">注册</a>
    </div>

    <div class="sign-up-container">
      <el-form ref="userForm" :model="params">
        <el-form-item
          class="input-prepend restyle"
        >
          <div>
            <el-input type="text" placeholder="你的昵称" v-model="params.nickname" @blur="checkNickName" />
            <i class="iconfont icon-user" />
          </div>

          <div class="btn" style="position:absolute;right: 0;top: 6px;width: 40%;">
            <span 
              v-show="tips_nickName"
              style="border: none;background-color: none;color:red"
            >请输入你的昵称</span>
          </div>
        </el-form-item>

        <el-form-item
          class="input-prepend restyle no-radius"
        >
          <div>
            <el-input type="text" placeholder="手机号" v-model="params.mobile" @blur="checkThePhone"/>
            <i class="iconfont icon-phone" />
          </div>

          <div class="btn" style="position:absolute;right: 0;top: 6px;width: 40%;">
            <span 
              v-show="tips_phone"
              style="border: none;background-color: none;color:red"
            >{{codePhone}}</span>
          </div>
        </el-form-item>

        <el-form-item
          class="input-prepend restyle no-radius"
        >
          <div style="width: 100%;display: block;float: left;position: relative">
            <el-input type="text" placeholder="验证码" v-model="params.securitycode" @blur="checkSecuritycode"/>
            <i class="iconfont icon-duanxin" />
          </div>

          <div class="btn" style="position:absolute;right: 30%;top: 6px;width: 40%;">
            <span 
              v-show="tips_code"
              style="border: none;background-color: none;color:red"
            >请输入验证码</span>
          </div>    

          <div class="btn" style="position:absolute;right: 0;top: 6px;width: 40%;">
            <a
              href="javascript:"
              type="button"
              @click="getCodeFun()"
              :value="codeTest"
              style="border: none;background-color: none"
            >{{codeTest}}</a>
          </div>

          
        </el-form-item>

        <el-form-item
          class="input-prepend"
        >
          <div>
            <el-input type="password" placeholder="设置密码" v-model="params.password" @blur="checkPassword"

             />
            <i class="iconfont icon-password" />
          </div>

          <div class="btn" style="position:absolute;right: 0;top: 6px;width: 40%;">
            <span 
              v-show="tips_password"
              style="border: none;background-color: none;color:red"
            >请输入密码</span>
          </div>  
        </el-form-item>

        <div class="btn">
          <input type="button" class="sign-up-button" value="注册"  @click="submitRegister()" />
        </div>
        <p class="sign-up-msg">
          点击 “注册” 即表示您同意并愿意遵守简书
          <br />
          <a target="_blank" href="http://www.jianshu.com/p/c44d171298ce">用户协议</a>
          和
          <a target="_blank" href="http://www.jianshu.com/p/2ov8x3">隐私政策</a> 。
        </p>
      </el-form>
      <!-- 更多注册方式 -->
      <div class="more-sign">
        <h6>社交帐号直接注册</h6>
        <ul>
          <li>
            <a id="weixin" class="weixin" target="_blank" href="https://open.weixin.qq.com/connect/qrconnect?appid=wxed9954c01bb89b47&redirect_uri=http://guli.shop/api/ucenter/wx/callback&response_type=code&scope=snsapi_login&state=mju#wechat_redirect" > 
             <i  class="iconfont icon-weixin" />
             </a>
          </li>
          <li>
            <a id="qq" class="qq" target="_blank" href="#" > 
             <i  class="iconfont icon-qq" />
             </a>
          </li>
          <li>
            <a id="gitee" class="gitee" target="_blank" href="http://127.0.0.1:8150/api/ucenter/gitee/login">
             <i class="iconfont icon-gitee-fill-round" style="color:red;"/></a>
             </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import "~/assets/css/sign.css";
import "~/assets/css/iconfont.css";

import registerApi from '@/api/register'

export default {
  layout: "sign",
  data() {
    
    return {
      params: {
        //封装注册输入数据
        mobile: "",//手机号
        securitycode: "", //验证码
        nickname: "",//昵称
        password: ""//密码
      },
      sending: true, //是否发送验证码
      second: 300, //倒计时间
      codeTest: "获取验证码",
      codePhone:"请输入手机号码",
      tips_nickName:false,
      tips_phone:false,
      tips_code:false,
      tips_password:false
    };
  },
  methods: {
    //注册提交的方法
    submitRegister() {
      registerApi.registerMember(this.params)
        .then(resp => {
          //提示注册成功
          this.$message({
            type: "success",
            message: "注册成功"
          });
          //跳转登录页面
          this.$router.push({ path: "/login" });
        })
        
    },
    //倒计时方法
    timeDown() {
      let result = setInterval(() => {
        --this.second;
        this.codeTest = this.second;
        if (this.second < 1) {
          clearInterval(result);
          this.sending = true;
          //this.disabled = false;
          this.second = 300;
          this.codeTest = "获取验证码";
        }
      }, 1000);
    },
    //通过输入手机号发送验证码
    getCodeFun() {
      registerApi.sendCode(this.params.mobile)
        .then(response => {
          this.sending = false;
          //调用倒计时的方法
          this.timeDown();
      });
    },

    checkNickName(){
      console.log(this.params.nickname)
      if(this.params.nickname=='' || this.params.nickname==null){
        // this.params.nickname.focus();
        this.tips_nickName = true;    
      }else{
        this.tips_nickName = false;
      }
    },

    checkThePhone(){
      if(this.params.mobile=='' || this.params.mobile==null){
        this.tips_phone = true;    
      }else{
        this.tips_phone = false;
        if (!/^1[34578]\d{9}$/.test((this.params.mobile))) {
          this.codePhone = "请输入正确手机号";
          this.tips_phone = true;
        }
      }
    },

    checkSecuritycode(){
      if(this.params.securitycode=='' || this.params.securitycode==null){
        // this.params.nickname.focus();
        this.tips_code = true;    
      }else{
        this.tips_code = false;
      }
    },

    checkPassword(){
      if(this.params.password=='' || this.params.password==null){
        // this.params.nickname.focus();
        this.tips_password = true;    
      }else{
        this.tips_password = false;
      }
    },

    checkAll(){
      if(this.tips_password == false && this.tips_code == false && this.tips_phone == false && this.tips_nickName == false){
        console.log('return true')
        this.doRegister = true
      }else{
        this.doRegister = false;
      }
    }

  }
};
</script>
